package com.itheima.service.impl;

import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ReportDao;
import com.itheima.entity.BusinessReportBean;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;

/**
 * 报表统计服务
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private ReportDao reportDao;

    //统计运营数据报表
    public Map<String, Object> getBusinessReport() throws Exception {

        //result.put("reportDate",today);
        //result.put("todayNewMember",todayNewMember);
        //result.put("totalMember",totalMember);
        //result.put("thisWeekNewMember",thisWeekNewMember);
        //result.put("thisMonthNewMember",thisMonthNewMember);
        //result.put("todayOrderNumber",todayOrderNumber);
        //result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        //result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        //result.put("todayVisitsNumber",todayVisitsNumber);
        //result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        //result.put("hotSetmeal",hotSetmeal);

        return null;
    }

    //统计套餐名和套餐数
    @Transactional
    @Override
    public List<Object> getSetmealReport() throws Exception {
        List<String> orederSetmealNames = reportDao.getOrederSetmealNames();
        List<Integer> orderSetmealCount = reportDao.getOrderSetmealCount();
        List<Object> rsList = new ArrayList<>();
        rsList.add(orederSetmealNames);
        rsList.add(orderSetmealCount);
        return rsList;
    }


    //获取一年内月份
    @Override
    public List<String> getMonthsOfLastYear() {
        LocalDate date = LocalDate.now();
        List<String> last12Months = new ArrayList<>();
        int month = date.getMonth().getValue();
        int year = date.getYear();
        int flag = 0;
        do {
            String monthYear = "" + year + "-" + (month <= 9 ? "0" + month : month);
            last12Months.add(monthYear);
            if (month == 1) {
                month = 13;
                year--;
            }
            month--;
            flag++;
        } while (flag < 12);

        //反转
        Collections.reverse(last12Months);

        return last12Months;
    }

    //    获取近一年内每月会员总数
    @Override
    public List<Integer> getMembersOfLastYear() {
        LocalDate date = LocalDate.now();
        List<Integer> memberCounts = new ArrayList<>();
        memberCounts.add(reportDao.getMemberOfLastyear(date));
        int month = date.getMonth().getValue();
        int year = date.getYear();
        int flag = 0;
        do {
            date = LocalDate.of(year, month, 1);
            
            memberCounts.add(reportDao.getMemberOfLastyear(date));
            if (month == 1) {
                month = 13;
                year--;
            }
            month--;
            flag++;
        } while (flag < 11);
        //反转
        Collections.reverse(memberCounts);

        return memberCounts;
    }


    //运营数据统计
    @Override
    public BusinessReportBean getBusinessReportData() {
        Integer totalMember = reportDao.totalMember();
        Integer newMember = reportDao.todayNewMember();
        Integer orderNumber = reportDao.todayOrderNumber();
        Integer visitsNumber = reportDao.todayVisitsNumber();
        Integer weekNewMember = reportDao.thisWeekNewMember();
        Integer weekOrderNumber = reportDao.thisWeekOrderNumber();
        Integer weekVisitsNumber = reportDao.thisWeekVisitsNumber();
        Integer monthNewMember = reportDao.thisMonthNewMember();
        Integer monthOrderNumber = reportDao.thisMonthOrderNumber();
        Integer monthVisitsNumber = reportDao.thisMonthVisitsNumber();
        List<Integer> orederSetmealNumberOrderByCount = reportDao.getOrederSetmealNumberOrderByCount();
        List<String> orederSetmealNamesOrderByCount = reportDao.getOrederSetmealNamesOrderByCount();
        Integer totalOrderNumber = reportDao.totalOrderNumber();
        BigDecimal orederSetmealNumber0 = BigDecimal.valueOf(orederSetmealNumberOrderByCount.get(0));
        BigDecimal orederSetmealNumber1 = BigDecimal.valueOf(orederSetmealNumberOrderByCount.get(1));
        BigDecimal totalOrderNumberBi = BigDecimal.valueOf(totalOrderNumber);
        //百分比1
        double percent1 = orederSetmealNumber0.divide(totalOrderNumberBi, 3, RoundingMode.HALF_UP).doubleValue();
//        BigDecimal  percent1= orederSetmealNumber0.divide(totalOrderNumberBi, 3, RoundingMode.HALF_UP);
        //百分比2
        double percent2 = orederSetmealNumber1.divide(totalOrderNumberBi, 3, RoundingMode.HALF_UP).doubleValue();
//        BigDecimal percent2 = orederSetmealNumber1.divide(totalOrderNumberBi, 3, RoundingMode.HALF_UP);
        //转化为hotSetmeals map
        List<HashMap<String, Object>> hotSetmeals = new ArrayList<>();
        HashMap<String, Object> hotSetmeal1 = new HashMap<>();
        HashMap<String, Object> hotSetmeal2 = new HashMap<>();
        hotSetmeal1.put("name", orederSetmealNamesOrderByCount.get(0));
        hotSetmeal1.put("setmeal_count", orederSetmealNumberOrderByCount.get(0));
        hotSetmeal1.put("proportion", percent1);
        hotSetmeal2.put("name", orederSetmealNamesOrderByCount.get(1));
        hotSetmeal2.put("setmeal_count", orederSetmealNumberOrderByCount.get(1));
        hotSetmeal2.put("proportion", percent2);
        hotSetmeals.add(hotSetmeal1);
        hotSetmeals.add(hotSetmeal2);
        //构造BusinessReportBean
        BusinessReportBean businessReportBean = new BusinessReportBean(
                newMember, weekNewMember, monthNewMember, totalMember, orderNumber, visitsNumber,
                weekOrderNumber, weekVisitsNumber, monthOrderNumber, monthVisitsNumber,
                LocalDate.now().toString(), hotSetmeals

        );


        return businessReportBean;
    }
//
//    @Override
//    public void downLoadXlsxWithTempalte(HttpServletRequest request, HttpServletResponse response) {
//
//    }


}
